
/* Usage:
	level_bar(string) or level_bar(float, int)

	current_level is how many full ("built") bullets to make, if current_level is a fraction it'll add a semi-bullet("building")
	max_level is how many total bullets to make, defaults to 10

	level_bar(5) will generate [full][full][full][full][full][empty][empty][empty][empty][empty]
	level_bar("2.5, 4") will generate [full][full][semi][empty]

*/
function level_bar(current_level, max_level) {

	if (isNaN(current_level) && max_level == undefined) {

		current_level = current_level.split(",");
		max_level = parseInt(current_level[1]);
		current_level = parseFloat(current_level[0]);

	}

	// Default max level is 10
	max_level = max_level || 10;

	// Error
	if (current_level > max_level) return "error";

	var level_bar_div = $("<div>").addClass("level_bar");

	for (var i=0; i<max_level; i++) {

		var background_position_x = "-42px" // Type of image (start and end cap, single bullet, middle segment)
		var background_position_y = 0; // Color of image (grey, neon-green or green)

		sub_div = $("<div />");

		if (max_level == 1) {
			background_position_x = 0; // Single sub_div
		} else if (i == 0) {
			background_position_x = "-22px"; // First sub_div
		} else if (i+1 == max_level) {
			background_position_x = "-62px"; // Last sub_div
		}

		if (current_level-i>0 && current_level-i<1) {
			background_position_y = "-44px"; // Building
		} else if (i<current_level) {
			background_position_y = "-22px"; // Built
		}

		$(sub_div).css({"background-position":background_position_x+" "+background_position_y});

		$(level_bar_div).append(sub_div);

	}

	return level_bar_div;

}

function add_seats(amount) {
	seats += amount;
	if (seats == 0) {

		// No change to seats
		$("#seat_display").css({"background-color":"transparent"}).text(number_format(facility_data["stadium"]["level"]));
		var btn = upgrade_button("disabled", pagecontent[47])
		$("#buttons").empty().html(btn);

	} else if (seats > 0) {

		// Upgrade
		$("#seat_display").css({"background-color":"#006600"}).text("+"+number_format(seats));
		var btn = upgrade_button(null, null, "stadium", seats);
		$("#buttons").empty().html(btn);

	} else if (seats < 0) {

		// Downgrade
		$("#seat_display").css({"background-color":"#8D2525"}).text(number_format(seats));
		$("#buttons").empty().html(downgrade_button(null, null, "stadium", seats));

	}

	var new_size = facility_data["stadium"]["level"]+seats;

	$("#new_stadium_size").text(number_format(new_size));
	$("#new_stadium_maintenance").text(number_format(new_size*seat_cost_per_week));
	if (seats > 0) {
		$("#construction_cost").text(number_format(seats*seat_cost_construction));
		$("#construction_time").text(Math.ceil(seats/seat_build_amount_per_day));
	} else {
		$("#construction_cost").text(0);
		$("#construction_time").text(0);
	}

}

preload("/pics/facilities_arrow.png");
preload("/pics/facilities_glow.png");

function make_facil_arrow(facil) {
	
	// New static arrow
	
	// Tutorial image is smaller, so don't show button
	if(typeof tutorial_facility_upgrade == "function") return false;

	var x = facility_data[facil]["coords"][0];
	var y = facility_data[facil]["coords"][1];

	$("#view .facil_arrow").remove();
	$("#view .facil_glow").remove();
	
	$("<img>")
		.addClass("facil_glow")
		.attr("src", "/pics/facilities_glow.png")
		.css({"margin-left":x-85+"px", "margin-top":y-72+"px"})
		.appendTo("#view");

	$("<img>")
		.addClass("facil_arrow")
		.attr("src", "/pics/facilities_arrow.png")
		.css({"margin-left":x-31+"px", "margin-top":y-108+"px"})
		.appendTo("#view");
	
	// Old animated arrow
	/*
	// Tutorial image is smaller, so don't show button
	if(typeof tutorial_facility_upgrade == "function") return false;

	var x = facility_data[facil]["coords"][0];
	var y = facility_data[facil]["coords"][1];


	if ($.browser.msie && $.browser.version < 9) {

		$("#view .facil_arrow").remove();
		$("#view .facil_glow").remove();

		// Legacy lal-browser :(
		$("<img>")
			.addClass("facil_glow")
			.attr("src", "/pics/facilities_glow.png")
			.css({"margin-left":x-85+"px", "margin-top":y-72+"px"})
			.appendTo("#view");

		$("<img>")
			.addClass("facil_arrow")
			.attr("src", "/pics/facilities_arrow.png")
			.css({"margin-left":x-31+"px", "margin-top":y-108+"px"})
			.appendTo("#view");

	} else {

		$("#view .facil_arrow").stop().fadeOut(function() { $(this).remove(); });
		$("#view .facil_glow").stop().fadeOut(function() { $(this).remove(); });

		$("<img>")
			.addClass("facil_glow")
			.attr("src", "/pics/facilities_glow.png")
			.css({"margin-left":x-85+"px", "margin-top":y-72+"px", "opacity":"0"})
			.animate({"opacity":"1"}, 1000, "easeOutBounce")
			.appendTo("#view");

		$("<img>")
			.addClass("facil_arrow")
			.attr("src", "/pics/facilities_arrow.png")
			.css({"margin-left":x-31+"px", "margin-top":y-158+"px"})
			.animate({"margin-top":y-108+"px"}, 1000, "easeOutBounce")
			.appendTo("#view");
	}
	
	*/
}

function change_page(force) {

	// New static page slide
	seats = 0;

	var facility_name = hash.get("page");
	facility_name = (facility_data[facility_name]) ? facility_name : "stadium";

	if (active_pane != facility_name || force) {

		active_pane = facility_name;

		// Clear all selected
		$(".facility_list li").removeClass("selected");
		
		$(".facility_list li."+facility_name).addClass("selected");
		
		$(".facility_details .holder").remove();
		
		make_facility_details(facility_name);
		
		if(facility_data[facility_name]["level"] > 0) {
			make_facil_arrow(facility_name);
		} else {
			$("#view .facil_arrow").remove();
			$("#view .facil_glow").remove();
		}
	}

	// Old animated page slide
	/*
	seats = 0;

	var facility_name = hash.get("page");
	facility_name = (facility_data[facility_name]) ? facility_name : "stadium";

	if (active_pane != facility_name || force) {

		active_pane = facility_name;

		// Clear all selected
		$(".facility_list li").removeClass("selected");

		// Arrow animation, removed since the change page effect was added
		//$(this).addClass("selected").find(".arrow").css({"width":"0px"}).animate({"width":"24px"}, 150, "easeOutElastic");

		$(".facility_list li."+facility_name).addClass("selected");
		$(".facility_details .holder").animate({"margin-left":"-470px"}, 500, function() { $(this).remove(); moveable=true; });

		make_facility_details(facility_name);
		
		if(facility_data[facility_name]["level"] > 0) {
			make_facil_arrow(facility_name);
		} else {
			$("#view .facil_arrow").remove();
			$("#view .facil_glow").remove();
		}
	}
	*/
}

function make_facility_list(select) {

	$(".facility_list ul").empty();

	for (var i in facility_data) {

		var val = facility_data[i];
		var level = val["level"];
		var level_building = (val["building"]) ? val["level"]-0.5 : val["level"];
		var max_level = val["level_cost"].length-1;

		var tmp_class = (i == select) ? "selected" : "";
//		var li = $("<li>").disableSelection()
		var li = $("<li>")
		.addClass("content_menu")
		.addClass(i)
		.addClass(tmp_class)
		.append(
			$("<div>").addClass("facility_list_element").append(
				$("<div>").addClass("arrow"),
				$("<div>").addClass("icon").css("background-image", "url('https://trophymanager.com/pics/facilities/icons/"+i+"_20px.png')"),
				$("<div>").addClass("text").text(val["title"]),
				((i == "stadium") ?
					$("<div>").addClass("large").css({"height":"24px", "padding-top":"6px"}).text(number_format(val["level"])+" "+pagecontent[52]) :
					$("<div>").html(level_bar(level_building, max_level)))
			)
		).click(function() {
			hash.set("page", this.i);
		});
		li[0].i = i;
		$(".facility_list ul").append(li);
	}

}

/*Args:
	state:disabled
	label:text
	facility:name
	level:new_level
*/
function upgrade_button(state, label, facility, level) {

	state = (state == "disabled") ? "disabled" : "";

	if (state == "disabled") {
		return $("<span>").addClass("button disabled").append(
			$("<span>").addClass("button_border").text(label)
		)
	}

	label = label || pagecontent[84];

	return $("<span>").addClass("button").addClass(state).append(
		$("<span>").addClass("button_border").css("background-image","url('https://trophymanager.com/pics/cf_mini_plus.png')").text(label)
	).click(function() {
		upgrade(facility, level)
	})

}

function downgrade_button(state, label, facility, level) {

	state = (state == "disabled") ? "disabled" : "";

	if (state == "disabled") {
		return $("<span>").addClass("button disabled").append(
			$("<span>").addClass("button_border").css("background-image","url('https://trophymanager.com/pics/small_red_minus.png')").text(label)
		)
	}

	label = label || pagecontent[77];

	return $("<span>").addClass("button").append(
		$("<span>").addClass("button_border").css("background-image","url('https://trophymanager.com/pics/small_red_minus.png')").text(label)
	).click(function() {
		downgrade(facility, level);
	})

}

function std_button(label) {
//	return $("<span>").addClass("button").disableSelection().append(
	return $("<span>").addClass("button").append(
		$("<span>").addClass("button_border").html(label)
	)
}

function make_facility_details(facility_name) {


	var val = facility_data[facility_name];
	var level = val["level"];
	var building = val["building"];
	var max_level = val["level_cost"].length-1;
	var downgradeable = val["downgradeable"];
	var title = facility_data[facility_name]["title"];

	function standard_details() {

		// Tutorial is compressed, so dont show up/downgrade table
		if(typeof tutorial_facility_upgrade == "function") return false;

		var maintenance = [0,0,0];
		// Downgrade maintenance
		if (downgradeable) {
			maintenance[0] = (level-2 < 0) ? 0 : number_format(val["maintenance"][level-2]);
		}
		// Current maintenance
		maintenance[1] = (level-1 < 0) ? 0 : number_format(val["maintenance"][level-1]);
		// Upgrade maintenance
		maintenance[2] = number_format(val["maintenance"][level]);


		return $("<table>").addClass("facil_details_scheme").attr("cellspacing",0).attr("cellpadding",0).append(
			function() {

				if (downgradeable && !level == 0) {
					return $("<tr>").addClass("subtle small").append(
						$("<td>").addClass("level").html(pagecontent[85]+"&nbsp;"+(level-1)),
						$("<td>").addClass("effect").html(val["level_effect"][level-1]),
						$("<td>").addClass("cost").css("white-space", "nowrap").html(
							pc_replace(pagecontent[48], {"[maintenance]":"<span class='coin'>"+maintenance[0]+"</span>"}) +"<br/>"+pc_replace(pagecontent[49], {"[cost]":"<span class='coin'>0</span>"})
						)
					)
				}
				return true;
			},

			$("<tr>").addClass("odd").append(
				//$("<td class='level'>").html("Level&nbsp;"+(level)+"&nbsp;of&nbsp;"+max_level),
				$("<td>").addClass("level").html(pagecontent[85]+"&nbsp;"+(level)),
				$("<td>").addClass("effect").html(val["level_effect"][level]+" "+facility_data[facility_name]["entity"]),
				$("<td>").addClass("cost").css("white-space", "nowrap").html(
					pc_replace(pagecontent[48], {"[maintenance]":"<span class='coin'>"+maintenance[1]+"</span>"})
				)
			)
		).append(
			function () {
				if (level < max_level) {
					return $("<tr>").addClass("subtle small").append(
						//$("<td class='level'>").html("Level&nbsp;"+(level+1)+"&nbsp;of&nbsp;"+max_level),
						$("<td>").addClass("level").html(pagecontent[85]+"&nbsp;"+(level+1)),
						$("<td>").addClass("effect").html(val["level_effect"][level+1]),
						$("<td>").addClass("cost").css("white-space", "nowrap").html(
							pc_replace(pagecontent[48], {"[maintenance]":"<span class='coin'>"+maintenance[2]+"</span>"})
							+"<br/>"+pc_replace(pagecontent[74], {"[cost]":"<span class='coin'>"+number_format(val["level_cost"][level+1])+"</span>"})
						)
					)
				}
				return true;
			}
		)
	}


	function seat_btn(label) {
//		return $("<span>").addClass("button seat").disableSelection().css({"margin":"0 2px"}).click(function() { add_seats(label) }).append(
		return $("<span>").addClass("button seat").css({"margin":"0 2px"}).click(function() { add_seats(label) }).append(
			$("<span>").addClass("button_border").css({"width":"50px", "padding":"0"}).text((label>0) ? "+"+label:label)
		)
	}

	function stadium_details() {
		var btns = $("<div>").addClass("text_center bottom_bump");
		if(!building)
		{
			btns.append(
				$("<div>").addClass("hand_drawn_warning clear_both").append(
					$("<div>").text(pagecontent[50])
				),
				seat_btn(-100),
				seat_btn(-10)
			);
		}
		btns.append(
				$("<span>").addClass("emboss_outer").css({"margin":"0 10px"}).append(
				$("<span>").attr("id","seat_display").addClass("embossed very_large").css({"padding":"8px 0"}).html(number_format(level)+" "+pagecontent[52])
			)
		);
		if(!building)
		{
			btns.append(
				seat_btn(10),
				seat_btn(100)
			);
		}
		var list ="";
		if(!building)
		{
			list = $("<ul>").addClass("clean clear_both stadium underlined_no_last bottom_bump").append(
				$("<li>").addClass("subtle no_border").append(
					$("<span>").addClass("label").text(pagecontent[51]),
					$("<span>").addClass("level").text(number_format(level)),
					$("<span>").addClass("type").text(pagecontent[52])
				),
				$("<li>").addClass("large").append(
					$("<span>").addClass("label").text(pagecontent[55]),
					$("<span>").attr("id", "new_stadium_size").addClass("level").text(number_format(level)),
					$("<span>").addClass("type").text(pagecontent[52])
				),
				$("<li>").addClass("subtle no_border").append(
					$("<span>").addClass("label").text(pagecontent[56]),
					$("<span>").addClass("coin level").css("width", "99px").text(number_format(level*seat_cost_per_week)),
					$("<span>").addClass("type").text(pagecontent[53])
				),
				$("<li>").addClass("large").append(
					$("<span>").addClass("label").text(pagecontent[57]),
					$("<span id='new_stadium_maintenance'>").addClass("coin level").css("width", "99px").text(number_format(level*seat_cost_per_week)),
					$("<span>").addClass("type").text(pagecontent[53])
				),
				$("<li>").addClass("large").append(
					$("<span>").addClass("label").text(pagecontent[58]),
					$("<span>").attr("id", "construction_cost").addClass("coin level").css("width", "99px").text(0),
					$("<span>").addClass("type")
				),
				$("<li>").addClass("large").append(
					$("<span>").addClass("label").text(pagecontent[59]),
					$("<span>").attr("id", "construction_time").addClass("level").text(0),
					$("<span>").addClass("type").text(pagecontent[54])
				)
			)
		}
		else
		{
			list = $("<ul>").addClass("clean clear_both stadium underlined").append(
					$("<li>").addClass("no_border large").append(
						$("<span class='label' >").text(pagecontent[60]),
						$("<span class='coin level' >").css("width", "99px").text(number_format(level*seat_cost_per_week)),
						$("<span class='type' >").text(pagecontent[53])
					)
				);
		}
		return $("<div>").append(btns).append(list);
	}

	function stadium_buttons() {
		return $("<div id='buttons'>").addClass("text_center bottom_bump").append(
			(building) ?
				$("<span>").addClass("building stadium_building").html(pc_replace(pagecontent[61], {"[size]":"<strong>"+number_format(level)+" "+pagecontent[52]+"</strong>", "[duration]":building})) :
				upgrade_button("disabled", pagecontent[62])
		)
	}

	/*
	var a = "";
	var b = new Object();
	b[0] = $("<div>")[0];
	b[1] = $("<div>")[0];
	for (var i in b) {
		a+=i+": "+b[i]+"\n";
	}
	alert(a);
	*/

	function standard_buttons() {
		return $("<div id='buttons'>").addClass("text_center bottom_bump").append(
			function() {

				// Already building?
				if (building) {
					return $("<span>").addClass('building').html(pc_replace(pagecontent[63], {"[level]":"<strong>"+pagecontent[85]+" "+level+"</strong>", "[duration]":building}));

				} else {
					// Add and/or disable downgrade button?
					if (val["downgradeable"]) {
						return (level > 0) ? downgrade_button(null, null, facility_name, (level-1)) : downgrade_button("disabled", pagecontent[64]);
					}
				}
				return true;
			}
		).append(
			function() {
				// Disable upgrade button?
				if (!building) {
					if (level == max_level) {
						return upgrade_button("disabled", pagecontent[65])
					} else if (manager_cash < val["level_cost"][val["level"]+1]) {
						return upgrade_button("disabled", pagecontent[66])
					}
					return upgrade_button("", pagecontent[84], facility_name, (level+1))
				}
			}
		)
	}

	var div = $("<div>").addClass("holder").append(
		$("<h2>").addClass("text_center underlined").text(function() {
			return (facility_name == "stadium") ? title : title+", "+pagecontent[67]+" "+facility_data[facility_name]["level"];
		}),
		$("<div>").addClass("bottom_bump").append(
			$("<div>").addClass("img_holder").css("backgroundImage","url('https://trophymanager.com/pics/facilities/icons/"+facility_name+".png')"),
			$("<span>").addClass("facility_text").text(facility_data[facility_name]["details"])
		),
		(facility_name == "stadium") ? stadium_details() : standard_details(),
		(facility_name == "stadium") ? stadium_buttons() : standard_buttons(),
		$("<div>").addClass("text_center").append(
//			$("<a>").addClass("arrow_right").attr("href", "/manual/"+facility_name).text(pc_replace(pagecontent[68], {"[facility]":title}))
		)
	)

	$(".facility_details").append(div);

}

function ajax_return(facility, level) {

	$.ajax({
		"type":"POST",
		"url":"/ajax/stadium_change.ajax.php",
		"dataType":"json",
		"data":{
			"facility":facility,
			"session_id": SESSION["id"],
			"level":level
		},
		"success":function(data) {
			if (data["page_refresh"]) {
				page_refresh();
				return false;
			}
			if (data["error"]) {

				modal.inner().empty().addClass("text_center").append(
					$("<div>").html(pagecontent[69]+"<br/>"+data["error"]),$("<br>"),
					std_button("Ok").click(function() { modal.hide() })
				);
				modal.banner();

			} else if (data["success"]) {

				var action;
				if (facility_data[data["facility"]]["level"] == data["level"]) {
					// error
				} else if (facility_data[data["facility"]]["level"] > data["level"]) {
					action = "downgrade";
				} else if (facility_data[data["facility"]]["level"] < data["level"]) {
					action = "upgrade";
				}

				// Overwrite with new data
				if(data["cash"]) manager_cash = data["cash"];
				// update buddy_list cash
				if(data["main_cash"]) $(".top_user_info .cash_block .coin").text(number_format(data["main_cash"]));

				facility_data[data["facility"]]["building"] = data["building"];
				facility_data[data["facility"]]["level"] = data["level"];

				// Change Image
/*				var src = "/pics/facilities/"+data["facility"]+"/"+data["image_level"]+".png"; // ** Lav noget image level for at tage hånd om stadium
				if(data["image_level"] > 0)
				{
					$("#image_"+data["facility"]).attr("src",src).show().removeClass("hidden");
				}
				else
				{
					$("#image_"+data["facility"]).hide();
				} */
				// Repop facility list and details
				make_facility_list();
				change_page(true);

				var $ok = std_button(global_content.ok).click(function() { 
						modal.hide(); 
						$("#view").attr("tabindex",-1).focus(); 
						show_stadium_image($(this).attr("facility"),$(this).attr("level"));
						}).attr("facility",data["facility"]+"").attr("level",data["image_level"]+"");
				// Confirm box
				modal.inner().empty().addClass("text_center").append(
					(action == "upgrade") ?
						$("<div>").text(pagecontent[70]) :
						$("<div>").text(pagecontent[71]),
					$("<br>"),
//					std_button(global_content.ok).click(function() { modal.hide(); $("#view").attr("tabindex",-1).focus(); })
					$ok
				);
				modal.banner();
				if(typeof tutorial_facility_upgrade == "function") tutorial_facility_upgrade(facility);
			} else {
				alert(dump(data));
			}
		}
	});
}

function upgrade(facility, level) {

	var facil_title = facility_data[facility]["title"]; // Get title
	var facil_cost = facility_data[facility]["level_cost"][level]; // Get cost
	var facil_time = level; // Time is level in days
	var facil_time_string = facil_time+" "+global_content["weeks"];

	if (facility == "stadium") {
		var delta_seats = level;
//		var delta_seats = level-facility_data[facility]["level"];
		facil_cost = number_format(delta_seats*seat_cost_construction);
		facil_time = Math.ceil(seats/seat_build_amount_per_day);
		facil_time_string = facil_time+" "+pagecontent[54];
		var header_text = pc_replace(pagecontent[72], {"[facility]":"<strong>"+facil_title+"</strong>", "[seatamount]":"<strong>"+number_format(level+facility_data[facility]["level"])+"</strong>"});
	}
	else
	{
		var header_text = pc_replace(pagecontent[73], {"[facility]":"<strong>"+facil_title+"</strong>", "[level]":"<strong>"+level+"</strong>"});
	}


	var html = $("<div>").addClass("popup").append(
		$("<div>").addClass("very_large").html(header_text),
		$("<hr>"),
		$("<div>").addClass("large").append(
			$("<span class='coin'>").text(pc_replace(pagecontent[74], {"[cost]":number_format(facil_cost)})),
			$("<br/>"),
			$("<span>").text(pc_replace(pagecontent[75], {"[time]":facil_time_string}))
		),
		$("<hr>"),
		std_button(pagecontent[76]+" <img src='https://trophymanager.com/pics/mini_green_check.png' />").css("margin-right", "4px").click(function() {
			modal.transition(get_loading_img(), function() {
				var level = facility_data[facility]["level"]+1;
				if (facility == "stadium") {
					level = facility_data[facility]["level"]+seats;
				}
				ajax_return(facility, level);
			})
		}),
		std_button(global_content["cancel"]+" <img src='https://trophymanager.com/pics/small_red_x.png' />").click(function() { modal.hide() })

	)

	modal.html(html);
	modal.show();
}

function downgrade(facility, level) {

	var facil_title = facility_data[facility]["title"]; // Get title

	var text = "<span class='warning bold'>"+pagecontent[77]+"</span> "+pc_replace(pagecontent[78], {"[facility]":"<strong>"+facil_title+"</strong>", "[level]":"<strong>"+level+"</strong>"});
	if (facility == "stadium") text = "<span class='warning bold'>"+pagecontent[77]+"</span> "+pc_replace(pagecontent[78], {"[facility]":"<strong>"+facil_title+"</strong>", "[level]":"<strong>"+number_format(parseInt(facility_data["stadium"]["level"])+level)+"</strong>"});

	var html = $("<div>").addClass("popup").append(
		$("<div>").addClass("very_large").html(text),
		$("<hr>"),
		$("<div>").addClass("large").append(
			$("<span>").addClass("coin").text(pagecontent[79]),
			$("<br/>"),
			$("<span>").text(pagecontent[80])
		),
		$("<hr>"),
		std_button(pagecontent[81]+" <img src='https://trophymanager.com/pics/mini_green_check.png' />").css("margin-right", "4px").click(function() {
			modal.transition(get_loading_img(), function() {
				var level = facility_data[facility]["level"]-1;
				if (facility == "stadium") {
					level = facility_data[facility]["level"]+seats;
				}
				ajax_return(facility, level);
			})
		}),
		std_button(pagecontent[82]+" <img src='https://trophymanager.com/pics/small_red_x.png' />").click(function() { modal.hide() })

	)

	modal.html(html);
	modal.show();

}


function show_stadium_image(facility,value)
{
	if(facility && value && value!="undefined")
	{
		if(facility=="stadium") image_array["sta"] = value;
		else facility_data[facility]["level"] = value;

	}
	else if(!value || value=="undefined")
	{
		value = facility_data[facility]["level"];
	}
	image_array["ff"] = facility_data["fastfood"]["level"];
	image_array["med"] = facility_data["medical"]["level"];
	image_array["msta"] = facility_data["merc_stand"]["level"];
	image_array["msto"] = facility_data["merc_store"]["level"];
	image_array["park"] = facility_data["parking"]["level"];
	image_array["phys"] = facility_data["physio"]["level"];
	image_array["res"] = facility_data["restaurant"]["level"];
	image_array["sau"] = facility_data["sausage"]["level"];
	image_array["toi"] = facility_data["toilets"]["level"];
	image_array["you"] = facility_data["youth"]["level"];
	image_array["toi"] = facility_data["toilets"]["level"];
	image_array["trai"] = facility_data["training"]["level"];
	image_array["ligh"] = facility_data["lights"]["level"];
	image_array["cov"] = facility_data["pitchcover"]["level"];
	image_array["drai"] = facility_data["draining"]["level"];
	image_array["spri"] = facility_data["sprinklers"]["level"];
	image_array["heat"] = facility_data["heating"]["level"];
	var tmp_ar = [];
	for(var i in image_array)
	{
		tmp_ar.push(i+"="+image_array[i]);
	}
	var img_str = "/pics/stadium_image.php?"+tmp_ar.join("&");
	if(facility)
	{
		var tmp_img = "/pics/facilities/"+facility+"/"+value+".png";
		$("<img />").attr("src",img_str).attr("tmp_src",tmp_img).hide().appendTo("body").load(function(){
			var $img = $("<img />").attr("main_src",$(this).attr("src")).attr("src",$(this).attr("tmp_src")).hide().appendTo("#view").load(function(){
				$(".facil_arrow").hide();
				$(".facil_glow").hide();
				$(this).fadeIn(function(){
					$(this).remove();
					$("#image_map").attr("src",$(this).attr("main_src"));
					$(".facil_arrow").show();
					$(".facil_glow").show();
				});
			}).error(function(){$("#image_map").attr("src",$(this).attr("main_src"))}).css({"position":"absolute"});
		});
	}
	else $("#image_map").attr("src",$(this).attr("main_src"));
//	return img_str;
}
